package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.dao.ChatMapper;
import com.dhu.pojo.Chat;
import com.dhu.pojo.Contacts;
import com.dhu.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class ChatController {
    @Autowired
    ChatService chatService;
    @Autowired
    ChatMapper chatMapper;

    @GetMapping("/getContacts")
    public String getContacts(int myId) {
        JSONObject res = new JSONObject();
        List<Contacts> contacts = chatService.getContacts(myId);
        if (contacts != null) {
            res.put("code", 200);
            res.put("contacts", contacts);
            res.put("message", "获取联系人成功");
        } else {
            res.put("code", 403);
            res.put("message", "获取联系人失败");
        }
        return res.toJSONString();
    }

    @GetMapping("/getMsg")
    public String getMsg(int myId, int hisId) {
        JSONObject res = new JSONObject();
        List<Chat> chatList = chatService.getMsg(myId, hisId);
        if (chatList != null) {
            if (chatList.size() != 0) {
                res.put("code", 200);
                res.put("msgList", chatList);
                res.put("message", "获取聊天信息成功");
            } else {
                res.put("code", 200);
                res.put("message", "没有聊天信息");
            }
        } else {
            res.put("code", 403);
            res.put("message", "获取聊天信息失败");
        }
        return res.toJSONString();
    }

    // 发送消息
    @RequestMapping("/sendMsg")
    public String sendMsg(@RequestBody Chat chat) {
        JSONObject res = new JSONObject();
        if (chatMapper.addChat(chat) == 1) {
            res.put("code", 200);
            res.put("message", "发送成功");
        } else {
            res.put("code", 403);
            res.put("message", "发送失败");
        }
        return res.toJSONString();
    }

    // 发送图片
    @RequestMapping("/sendImg")
    public String sendImg(Chat chat, @RequestParam("file") MultipartFile file) {
        JSONObject res = new JSONObject();
        try {
            String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName;
            file.transferTo(new File(realPath, newName));
            chat.setImage(newName);
            if (chatMapper.sendImg(chat) == 1) {
                res.put("code", 200);
                res.put("message", "发送成功");
            } else {
                res.put("code", 403);
                res.put("message", "发送失败");
            }
        } catch (Exception e) {
            res.put("code", 402);
            res.put("message", "上传文件失败");
        }
        return res.toJSONString();
    }
}
